import os
from datetime import datetime
from data_wash.utils import avg_completion_data,data_filter

WORK_PATH = os.getcwd() #获取当前项目工作目录，
DATA_PATH = os.path.join(WORK_PATH, 'meteorological_data') #数据目录

# interpolate from ：
interpolate_from = 'QINGHAI_DATA\QingHai.txt'

# interpolate to ：
interpolate_to = 'QINGHAI_DATA\QingHai_data_interpolated_index9-16.txt'

file_object_path = os.path.join(DATA_PATH, interpolate_from) #    'GANSU_MONTH_DATA\gansu_data.txt'
with open(file_object_path,'r') as file_object:
    lines = file_object.readlines()

DATA_TABLE_HEAD = lines.pop(0)
print('剔除数据表头:')
print(DATA_TABLE_HEAD)

'''
file_data = [
    [[],[],..... ],  // station_1
    [[],[],..... ],  // station_2
    [[],[],..... ],  // station_3
    [  // station_4
        ['52533','1951','1',.......],
        ['52533','1951','2',.......],
        ['52533','1951','3',.......],
        ..... 
    ],
]
'''

file_data = []  # 三维数组 读取文件的列表
temp_station_number = '0'
temp_station_data = []
for line in lines:

    read_row = line.split() #空格作为分隔符对line一行进行切片
    station_number_in_row = read_row[0] #获得站点号

    if temp_station_number == '0':
        temp_station_number = station_number_in_row
        # print(temp_station_number+'change')

    if temp_station_number != station_number_in_row:
        #不是同一个站点的数据
        file_data.append(temp_station_data)
        temp_station_data = []
        temp_station_number = station_number_in_row
        # print(temp_station_number + 'change')

    temp_station_data.append(read_row)  # 添加某个站点的数据

file_data.append(temp_station_data) #追加最后一个站点的数据



print('【站点信息】 该文件共有站点' + str(len(file_data)) + '个：')
for station in file_data:
    sta_len = len(station)-1
    print('站点：' + station[0][0] + ', 时间：' + station[0][1]+'/'+station[0][2] + '-' + station[sta_len][1]+'/'+station[sta_len][2] )


def data_fill_by_index(file_data, station, row_data, year, month, index, label_name=''):
    '''
    填充数据，如果不是都空。
    :param file_data: 整个文件数据集
    :param station: 所有站
    :param row_data: 某个station站中的 一行数据
    :param year:
    :param month:
    :param index: 改行的索引
    :param label_name: 该列的中文名称
    :return: 1.None:all things done well  2.'del_row_data': delete this row of data
    '''
    # row_data[index] 's data item

    if data_filter(row_data[index]) == 'NaN':
        print('站点(%s) %s年%s月  【%s】数据缺失'  %( str(station[0][0]), str(year), str(month), str(label_name),) )
        # python for item in list  中 ‘item’是直接索引地址而不是复制，所以可以直接赋值
        caculated_data = avg_completion_data(file_data, year, month,index)
        if caculated_data is not None:
            row_data[index] = str(round(caculated_data, 6))  # 小数点后保留6位
        else:
            print('由于所有站点数据缺失，删除本行 数据:', str(station[0][0]), '-', str(year),'-', str(month),'-', str(label_name) )
            return 'del_row_data'
        print('补后为: ' + str(row_data[index]) )
        return None


file_data_write = [] # give a init array to prepear to write in to file

print('1) 【数据插补】')
for station in file_data:
    print('\n 站点：' + station[0][0] + '数据检测、插补：' )
    init_station = []
    for row_data in station:
        # 缺失数据的年月
        year = row_data[1]
        month = row_data[2]

        # data_fill_by_index(file_data, station, row_data, year, month, 9, '降水')
        # data_fill_by_index(file_data, station, row_data, year, month, 10, '平均气压')
        # data_fill_by_index(file_data, station, row_data, year, month, 12, '平均气温')
        # data_fill_by_index(file_data, station, row_data, year, month, 13, '平均水气压')
        # data_fill_by_index(file_data, station, row_data, year, month, 14, '平均相对湿度')
        # data_fill_by_index(file_data, station, row_data, year, month, 15, '平均最低气温')
        # data_fill_by_index(file_data, station, row_data, year, month, 16, '平均最高气温')

        if data_fill_by_index(file_data, station, row_data, year, month, 9, '降水') == 'del_row_data':
            continue
        if data_fill_by_index(file_data, station, row_data, year, month, 10, '平均气压') == 'del_row_data':
            continue
        if data_fill_by_index(file_data, station, row_data, year, month, 12, '平均气温') == 'del_row_data':
            continue
        if data_fill_by_index(file_data, station, row_data, year, month, 13, '平均水气压') == 'del_row_data':
            continue
        if data_fill_by_index(file_data, station, row_data, year, month, 14, '平均相对湿度') == 'del_row_data':
            continue
        if data_fill_by_index(file_data, station, row_data, year, month, 15, '平均最低气温') == 'del_row_data':
            continue
        if data_fill_by_index(file_data, station, row_data, year, month, 16, '平均最高气温') == 'del_row_data':
            continue
        # don't have data need to be removed ,so append:
        init_station.append(row_data)
    file_data_write.append(init_station)


print('2) 【相邻站点全都缺失的补充为‘data_filter()过滤后的实际数值’】')
for station in file_data_write:
    print('站点：' + station[0][0] + '数据检测、插补：' )
    for row_data in station:

        row_data[9] = data_filter(row_data[9])
        row_data[10] = data_filter(row_data[10])
        row_data[12] = data_filter(row_data[12])
        row_data[13] = data_filter(row_data[13])
        row_data[14] = data_filter(row_data[14])
        row_data[15] = data_filter(row_data[15])
        row_data[16] = data_filter(row_data[16])



print('3) 【数据打印至文件......】')
write_file_path = os.path.join(DATA_PATH, interpolate_to ) # 'GANSU_MONTH_DATA\gansu_data_interpolated_index9-16.txt'
write_file = open(write_file_path, 'a+')

#打印表头
write_file.write(DATA_TABLE_HEAD)

#打印数据：
for station in file_data_write:
    print('站点：' + station[0][0] + '数据打印......')
    for row_data in station:
        for item in row_data:
            write_file.write(item + ' ')
        write_file.write('\n')

print('数据已输出至：'+ write_file_path)

file_object.close()
write_file.close()